import * as cc from "cc";
import yyComponent from "../../Script/Common/yyComponent";
import { GlobalEnum } from "../../Script/GameSpecial/GlobalEnum";
import { EventType } from "../../Script/GameSpecial/GameEventType";
import GlobalData from "../../Script/Common/GlobalData";
import PlayerData from "../../Script/Common/PlayerData";

const { ccclass, property } = cc._decorator;

@ccclass
export default class WinUI extends yyComponent {

    /**场景/UI类型 */
    public get uiType() { return GlobalEnum.UI.winUI; }

    public init(data?) {
        this.initGold();
        this.onEvents();
        if (undefined !== data) {
            this.setData(data);
        }
    }
    protected setData(data) {
        this.gold = data.gold;
        this.goldLabel.string = this.gold.toString();
    }

    protected onEvents() {
        this.node.on(cc.Node.EventType.TOUCH_END, this.onNodeTouchEnd, this);
        this.on(EventType.LevelEvent.stopJoy, this.onStopJoy, this);
    }

    public reset() {
        this.resetGold();
    }

    public show(data?: { data?: any, config?: any }) {
        this.reset();
        this.node.active = true;
        if (undefined !== data && undefined !== data.data) {
            this.setData(data.data);
        }
        // this.emit(EventType.Common.AudioEvent.playEffect, GlobalEnum.AudioClip.win);
        this.emit(EventType.Common.UIEvent.enter, GlobalEnum.UI.playerAssetBar);
        this.unlockSkin();
        this.onEvents();
    }


    //#region 奖励内容
    @property(cc.Label)
    protected goldLabel: cc.Label = null;
    protected gold: number = 200;
    protected awarded: boolean = false;
    protected initGold() {
        this.awarded = false;
        this.goldLabel.string = "";
    }
    protected resetGold() {
        this.awarded = false;
        this.goldLabel.string = "";
    }
    //#endregion

    /*******************************************************按钮回调*******************************************************/
    protected continueGame() {
        if (this.awarded) {
            this.enterLobby();
        } else {
            this.addGold(this.gold, this.enterLobby.bind(this));
        }
    }
    protected onBtnGetAward() {
        this.playClickBtn();
        this.continueGame();
    }
    //视频多倍领取
    protected onBtnVideo() {
        this.playClickBtn();
        this.getAwardByVideo();
    }
    protected getAwardByVideo() {
        this.emit(EventType.Common.SDKEvent.showVideo, {
            success: () => {
                this.addGold(this.gold * 3, this.getGoldByVideoFinish.bind(this));
            },
        });
    }
    //多倍领取完毕
    protected getGoldByVideoFinish() {
        this.continueGame();
    }

    /**播放金币动画获得金币 */
    protected addGold(gold: number, cb?: Function) {
        this.emit(EventType.Common.UIEvent.playGoldAnim, {
            cb: () => {
                this.emit(EventType.Common.PlayerDataEvent.updatePlayerData, {
                    attribute: "gameData.asset.gold",
                    value: gold,
                    mode: "+",
                    emit: true,
                    save: true,
                });
                this.awarded = true;
                !!cb && cb();
            },
            gold: gold,
        })
    }

    protected enterLobby() {
        this.emit(EventType.Common.PlayerDataEvent.trySkinEnd);
        this.emit(EventType.Common.GameFlow.enterGameFlow, GlobalEnum.Common.GameFlowStep.recommendDialog);
    }

    protected onBtnMoreGame() {
        this.emit(EventType.Common.SDKEvent.showAppBox);
    }
    protected onBtnShare() {
        this.emit(EventType.Common.SDKEvent.shareToFriends);
    }

    @property(cc.Node)
    skinAward: cc.Node = null;
    @property(cc.Label)
    unlockPlan: cc.Label = null;
    unLockShow: boolean = false;
    unlockSkin() {
        let plan = PlayerData.getData("gameData.unlockSkin");
        if (plan < 1) {
            this.skinAward.active = true;
            this.nowRote = plan;
            this.goalRote = plan + 0.2;
            this.unLockShow = false;
            this.unlockPlan.string = Math.floor(this.nowRote * 100).toString();
            this.maskUITrans.height = 260 * (1 - this.nowRote);
            setTimeout(() => {
                this.unLockShow = true;
            }, 1500);
        } else {
            this.unLockShow = false;
            this.skinAward.active = false;
        }
    }
    addUnlockPlan() {
        let plan = PlayerData.getData("gameData.unlockSkin");
        plan += 0.2;
        this.emit(EventType.Common.PlayerDataEvent.updatePlayerData, {
            attribute: "gameData.unlockSkin",
            value: plan,
            mode: "=",
            emit: true,
            save: true,
        });
        if (plan >= 1) {
            this.award.active = true;
            this.emit(EventType.Common.PlayerDataEvent.updatePlayerData, {
                attribute: "gameData.PlayerSkin.owned",
                value: 11,
                mode: "push",
                emit: true,
                save: false,
            });
            this.emit(EventType.Common.PlayerDataEvent.updatePlayerData, {
                attribute: "gameData.PlayerSkin.cur",
                value: 11,
                mode: "=",
                emit: true,
                save: true,
            });

        }
    }
    maskHight = 260;
    nowRote = 0;
    goalRote = 0;
    @property(cc.UITransform)
    maskUITrans: cc.UITransform = null;
    update(dt) {
        if (!!this.unLockShow) {
            if (this.nowRote < this.goalRote) {
                this.nowRote += dt * 0.2;
                this.unlockPlan.string = Math.floor(this.nowRote * 100).toString();
                this.maskUITrans.height = 214 * (1 - this.nowRote);
            } else {
                this.nowRote = this.goalRote;
                this.unlockPlan.string = Math.floor(this.nowRote * 100).toString();
                this.maskUITrans.height = 214 * (1 - this.nowRote);
                this.unLockShow = false;
                this.addUnlockPlan();
            }
        }
    }
    @property(cc.Node)
    award: cc.Node = null;
    onBtnExitAward() {
        this.award.active = false;
    }



    protected onStopJoy(p: cc.Vec2) {
        if (p.y < 320) {
            this.emit(EventType.Common.HeyGameSDK.clickNativeAd);
        }
    }

    protected onNodeTouchEnd(e: cc.EventTouch) {
        let p = e.getUILocation();
        this.onStopJoy(p);
    }
}
